package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.SafePayBusinessAuthAuditRecordVo;
import com.net.xpay.common.domain.SafePayBusinessAuthAuditRecord;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.manager.SafePayBusinessAuthAuditRecordManager;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.manager.partner.PartnerManager;
import com.net.xpay.common.service.SafePayBusinessAuthAuditRecordService;
import com.net.xpay.core.model.AdminSafePayBookAccountAuditModel;
import com.net.xpay.core.model.AdminSafePayBookAccountSearchModel;
import com.net.xpay.core.model.AdminSafePayBookAccountUpdateModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/safe-pay-business-auth-audit-record")
@Api(tags = "运营/支付宝账号", description = "运营 支付宝账号 net")
public class AdminSafePayBusinessAuthAuditRecordController {

    @Autowired
    private SafePayBusinessAuthAuditRecordManager safePayBusinessAuthAuditRecordManager;

    @Autowired
    private SafePayBusinessAuthAuditRecordService safePayBusinessAuthAuditRecordService;

    @Autowired
    private UserManager userManager;

    @Autowired
    private PartnerManager partnerManager;

    @ApiOperation("搜索安全发账本账户资质审核记录")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-business-auth-audit-record::search')")
    public ResultPagedList<SafePayBusinessAuthAuditRecordVo> searchSafePayBusinessAuthAuditRecord(@RequestBody AdminSafePayBookAccountSearchModel searchModel) {
        List<SafePayBusinessAuthAuditRecord> records = safePayBusinessAuthAuditRecordManager.search(searchModel);

        List<SafePayBusinessAuthAuditRecordVo> vos = BeanCopierUtil.batchCopy(SafePayBusinessAuthAuditRecord.class, SafePayBusinessAuthAuditRecordVo.class, records);
        for (SafePayBusinessAuthAuditRecordVo vo : vos) {
            User user = userManager.getById(vo.getUserId());

            vo.setUsername(user.getName());
            if (user.getPartnerId() != null) {
                Partner partner = partnerManager.getById(user.getPartnerId());
                vo.setPartnerId(partner.getId());
                vo.setPartnerName(partner.getName());
            }
        }
        long count = safePayBusinessAuthAuditRecordManager.searchCount(searchModel);
        return new ResultPagedList<>(vos, count, searchModel);
    }

    @ApiOperation("审核通过")
    @PostMapping("/audit-success")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-business-auth-audit-record::audit-success')")
    public ResultData<Boolean> auditSuccess(Long id) {
        safePayBusinessAuthAuditRecordService.auditSuccess(id);
        return new ResultData<>(true);
    }

    @ApiOperation("审核失败")
    @PostMapping("/audit-fail")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-business-auth-audit-record::audit-fail')")
    public ResultData<Boolean> auditFail(@RequestBody AdminSafePayBookAccountAuditModel model) {
        safePayBusinessAuthAuditRecordService.auditFail(model);
        return new ResultData<>(true);
    }

    @ApiOperation("修改信息")
    @PostMapping("/update-info")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'safe-pay-business-auth-audit-record::update-info')")
    public ResultData<Boolean> updateInfo(@RequestBody AdminSafePayBookAccountUpdateModel model) {
        safePayBusinessAuthAuditRecordService.updateInfo(model);
        return new ResultData<>(true);
    }
}
